﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

namespace Directory.Web
{
	public partial class ItemView : System.Web.UI.Page
	{
		protected int pageIndex;
		protected int pageSize = WebSettings.GetInstance().ItemViewPageSize;
		protected int itemsRecords;
		protected int categoryId;
		protected string LogoBasePath = ConfigurationManager.AppSettings["LogoBasePath"].Replace("~/", "/");
		private string pageKey = "page";
		protected string categoryIdKey = "categoryId";
		protected Directory.Model.CategoryInfo currentCategory;

		protected void Page_Load(object sender, EventArgs e)
		{
			this.CategoryNavTree.QueryKey = this.categoryIdKey;
			if (!int.TryParse(Request.QueryString[categoryIdKey], out categoryId))
			{
				categoryId = WebSettings.GetInstance().RootCategoryId;
			}
			currentCategory = CategoryDataProxy.GetCategory(categoryId);
			if (!IsPostBack)
			{
				rptCategoryNav.DataSource = CategoryDataProxy.GetRecursiveParentsOfCategory(categoryId);
				rptCategoryNav.DataBind();

				if (this.CateogryChilds != null)
				{
					this.CateogryChilds.CategoryId = categoryId;
				}

				if (!int.TryParse(Request.QueryString[pageKey], out pageIndex))
				{
					pageIndex = 0;
				}
				rptItemsList.DataSource = ItemDataProxy.GetItemsInCategory(categoryId, pageIndex, WebSettings.GetInstance().ItemViewPageSize, out itemsRecords);
				rptItemsList.DataBind();

				SetPageNav();
			}
		}

		private string GetPageNavigateUrl(int _pageIndex)
		{
			return HttpContext.Current.Request.Url.AbsolutePath + "?" + categoryIdKey + "=" + categoryId.ToString() + "&" + pageKey + "=" + _pageIndex.ToString();
		}

		private void SetPageNav()
		{
			int pageCounts = ((itemsRecords - 1) / pageSize) + 1;
			lbCurrentPage.Text = (pageIndex + 1) + " / " + pageCounts;

			if (pageCounts < 1)
			{
				hplFirstPage.Enabled = false;
				hplPrePage.Enabled = false;
				hplNextPage.Enabled = false;
				hplLastPage.Enabled = false;
			}
			else if (pageIndex == 0)
			{
				hplFirstPage.Enabled = false;
				hplPrePage.Enabled = false;
				if (pageCounts == 1)
				{
					hplNextPage.Enabled = false;
					hplLastPage.Enabled = false;
				}
				else
				{
					hplNextPage.Enabled = true;
					hplNextPage.NavigateUrl = GetPageNavigateUrl(pageIndex + 1);
					hplLastPage.Enabled = true;
					hplLastPage.NavigateUrl = GetPageNavigateUrl(pageCounts - 1);
				}
			}
			else if (pageIndex >= pageCounts - 1)
			{
				pageIndex = pageCounts - 1;
				hplFirstPage.Enabled = true;
				hplFirstPage.NavigateUrl = GetPageNavigateUrl(0);
				hplPrePage.Enabled = true;
				hplPrePage.NavigateUrl = GetPageNavigateUrl(pageIndex - 1);
				hplNextPage.Enabled = false;
				hplLastPage.Enabled = false;
			}
			else
			{
				hplFirstPage.Enabled = true;
				hplFirstPage.NavigateUrl = GetPageNavigateUrl(0);
				hplPrePage.Enabled = true;
				hplPrePage.NavigateUrl = GetPageNavigateUrl(pageIndex - 1);
				hplNextPage.Enabled = true;
				hplNextPage.NavigateUrl = GetPageNavigateUrl(pageIndex + 1);
				hplLastPage.Enabled = true;
				hplLastPage.NavigateUrl = GetPageNavigateUrl(pageCounts - 1);
			}
		}
	}
}
